package com.usaas.search.service.impl;
import java.util.Date;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.usaas.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.util.StrUtil;
import com.usaas.search.mapper.PlatMessageCommentsMapper;
import com.usaas.search.pojo.PlatMessageCommentsPojo;
import com.usaas.search.dto.PlatMessageCommentsDto;
import com.usaas.search.service.IPlatMessageCommentsService;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.collection.ListUtil;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * 平台推广信息—留言Service业务层处理
 * 
 * @author Helios
 * @date 2024-08-14
 */
@Slf4j
@Service
public class PlatMessageCommentsServiceImpl extends ServiceImpl<PlatMessageCommentsMapper,PlatMessageCommentsPojo> implements IPlatMessageCommentsService {

    @Resource
    private PlatMessageCommentsMapper platMessageCommentsMapper;

    /**
     * 查询平台推广信息—留言
     * 
     * @param id 平台推广信息—留言主键
     * @return 平台推广信息—留言
     */
    @Override
    public PlatMessageCommentsDto queryById(Long id) {
        PlatMessageCommentsPojo pojo = this.getById(id);
        if (pojo != null) {
            PlatMessageCommentsDto dto= BeanUtil.copyProperties(pojo, PlatMessageCommentsDto.class);
            List<PlatMessageCommentsDto> dtos = ListUtil.of(dto);
            convertList(dtos);
            return dtos.get(0);
        }
        return null;
    }

    /**
     * 查询平台推广信息—留言列表
     * 
     * @param platMessageCommentsDto 平台推广信息—留言
     * @return 平台推广信息—留言
     */
    @Override
    public List<PlatMessageCommentsDto> queryList(PlatMessageCommentsDto platMessageCommentsDto) {
        LambdaQueryWrapper<PlatMessageCommentsPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper =this.buildWrapper(queryWrapper, platMessageCommentsDto);
        if (queryWrapper == null) {
            return null;
        }
        List<PlatMessageCommentsPojo> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        List<PlatMessageCommentsDto> dtoList = BeanUtil.copyToList(list, PlatMessageCommentsDto.class);
        convertList(dtoList);
        return dtoList;
    }

    private void convertList(List<PlatMessageCommentsDto> dtoList) {
        if(CollectionUtil.isEmpty(dtoList)){
            return;
        }
    }

    /**
     * 新增平台推广信息—留言
     * 
     * @param platMessageCommentsDto 平台推广信息—留言
     * @return 结果
     */
    @Override
    @Transactional
    public boolean add(PlatMessageCommentsDto platMessageCommentsDto) {

        PlatMessageCommentsPojo platMessageCommentsPojo = BeanUtil.copyProperties(platMessageCommentsDto, PlatMessageCommentsPojo.class);
    return this.save(platMessageCommentsPojo);
    }

    /**
     * 修改平台推广信息—留言
     * 
     * @param platMessageCommentsDto 平台推广信息—留言
     * @return 结果
     */
    @Override
    @Transactional
    public boolean update(PlatMessageCommentsDto platMessageCommentsDto) {
            PlatMessageCommentsPojo platMessageCommentsPojo = BeanUtil.copyProperties(platMessageCommentsDto, PlatMessageCommentsPojo.class);
        return this.updateById(platMessageCommentsPojo);
    }

    /**
     * 批量删除平台推广信息—留言
     *
     * @param ids 需要删除的平台推广信息—留言主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeBatch(List<String> ids) {
        List<Long> idsLong = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return this.removeBatchByIds(idsLong);
    }



    /**
     * 分页查询${subTable.functionName}信息
     * @param platMessageCommentsDto
     * @return
     */
    @Override
    public AjaxResult queryPage(PlatMessageCommentsDto platMessageCommentsDto) {
        Page<PlatMessageCommentsPojo> page = new Page<>(platMessageCommentsDto.getPageNum(), platMessageCommentsDto.getPageSize());
        LambdaQueryWrapper<PlatMessageCommentsPojo> wrapper = new LambdaQueryWrapper<PlatMessageCommentsPojo>();
        //此处可做前置判断
        if(wrapper==null){
            return AjaxResult.success(page);
        }
        wrapper=buildWrapper(wrapper,platMessageCommentsDto);
        if(wrapper==null){
            return AjaxResult.success(page);
        }
        Page<PlatMessageCommentsPojo> pageResult = this.page(page, wrapper);
        Page<PlatMessageCommentsDto> finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatMessageCommentsPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatMessageCommentsDto> dtos = BeanUtil.copyToList(records, PlatMessageCommentsDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return AjaxResult.success(finalResult);
    }

    private LambdaQueryWrapper<PlatMessageCommentsPojo> buildWrapper(LambdaQueryWrapper<PlatMessageCommentsPojo> wrapper, PlatMessageCommentsDto param) {
        log.info("buildPageWrapper param :{}", param);
        if (wrapper == null) {
            return null;
        }
            Long id=param.getId();
                if (id !=null){
                    wrapper.eq(PlatMessageCommentsPojo::getId, id);
            }
            Long parentId=param.getParentId();
                if (parentId !=null){
                    wrapper.eq(PlatMessageCommentsPojo::getParentId, parentId);
            }
            Long messageId=param.getMessageId();
                if (messageId !=null){
                    wrapper.eq(PlatMessageCommentsPojo::getMessageId, messageId);
            }
            Long commentUserId=param.getCommentUserId();
                if (commentUserId !=null){
                    wrapper.eq(PlatMessageCommentsPojo::getCommentUserId, commentUserId);
            }
            String commentUserName=param.getCommentUserName();
                if (StrUtil.isNotBlank(commentUserName)) {
                    wrapper.eq(PlatMessageCommentsPojo::getCommentUserName, commentUserName);
                }
            String commentInfo=param.getCommentInfo();
                if (StrUtil.isNotBlank(commentInfo)) {
                    wrapper.eq(PlatMessageCommentsPojo::getCommentInfo, commentInfo);
                }
            String createBy=param.getCreateBy();
                if (StrUtil.isNotBlank(createBy)) {
                    wrapper.eq(PlatMessageCommentsPojo::getCreateBy, createBy);
                }
            Date createTime=param.getCreateTime();
                if (createTime !=null){
                    wrapper.eq(PlatMessageCommentsPojo::getCreateTime, createTime);
            }
            String updateBy=param.getUpdateBy();
                if (StrUtil.isNotBlank(updateBy)) {
                    wrapper.eq(PlatMessageCommentsPojo::getUpdateBy, updateBy);
                }
            Date updateTime=param.getUpdateTime();
                if (updateTime !=null){
                    wrapper.eq(PlatMessageCommentsPojo::getUpdateTime, updateTime);
            }
            String remark=param.getRemark();
                if (StrUtil.isNotBlank(remark)) {
                    wrapper.eq(PlatMessageCommentsPojo::getRemark, remark);
                }
            Long delFlag=param.getDelFlag();
                if (delFlag !=null){
                    wrapper.eq(PlatMessageCommentsPojo::getDelFlag, delFlag);
            }
        log.info("buildPageWrapper result :{}", wrapper);
        return wrapper;
    }
}
